package com.auto.learning.activity;

import java.io.Serializable;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.auto.learning.MainActivity;
import com.auto.learning.R;
import com.auto.learning.bean.BannerBean;
import com.auto.learning.bean.BookBean;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.ViewImagePagerUtil;
import com.auto.learning.view.SharedPreferencesUtils;

/**
 * 导航页面
 * @author wangjianjun
 */
public class NavigationActivity extends Activity {
	private ArrayList<BannerBean> bannerList = new ArrayList<BannerBean>();
	private ArrayList<BookBean> bookList = new ArrayList<BookBean>();
	
	private ViewPager viewPager;
	
	private int[] imageUrls = {}; 
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_navigation);
		initView();
	}
	
	
	ViewImagePagerUtil imagePagerUtil;
	LinearLayout home_ads_indicator;
	private void initView(){
		SharedPreferencesUtils.setParam(this, "isFirst", false);
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		home_ads_indicator = (LinearLayout) findViewById(R.id.home_ads_indicator);
		ArrayList<View> _imageView = new ArrayList<View>();
		for (int i = 0; i < imageUrls.length; i++) {
			_imageView.add(buildADItemView(imageUrls[i], i == imageUrls.length - 1));
		}
		viewPager.removeAllViews();
		home_ads_indicator.removeAllViews();
		imagePagerUtil = new ViewImagePagerUtil(this, _imageView, viewPager, home_ads_indicator,handler);
		imagePagerUtil.buildSlideView();
		imagePagerUtil.toPage1();
		imagePagerUtil.setCycle(false);
	}
	
	private View buildADItemView(int adId,boolean isLast) {
		ImageView imv = (ImageView) LayoutInflater.from(this).inflate(R.layout.ad_child_view, null);
		imv.setImageBitmap(CommonMethod.readBitMap(NavigationActivity.this, adId));
//		imv.setImageResource(adId);
		if (isLast) {
			imv.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(NavigationActivity.this,MainActivity.class);
					intent.putExtra("bannerList", (Serializable)bannerList);
					intent.putExtra("guessbookList", (Serializable)bookList);
					startActivity(intent);
					finish();
				}
			});
		}
		return imv;
	}
}
